using Microsoft.AspNetCore.Mvc;
using SheLife.Application.Interfaces;
using SheLife.Domain.Entities;

namespace SheLife.Api.Controllers
{
    /// <summary>
    /// 用户管理控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class UsersController : ControllerBase
    {
        private readonly IUserService _userService;

        public UsersController(IUserService userService)
        {
            _userService = userService;
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <returns>用户信息</returns>
        [HttpGet("{id}")]
        public async Task<IActionResult> GetUser(Guid id)
        {
            var user = await _userService.GetUserByIdAsync(id);
            if (user == null)
            {
                return NotFound();
            }

            // 返回用户信息，但不包含敏感数据如密码哈希
            return Ok(new
            {
                user.Id,
                user.Username,
                user.Email,
                user.Nickname,
                user.AvatarUrl,
                user.Birthday,
                user.LifeStage,
                user.IsEmailVerified,
                user.IsActive,
                user.LastLoginAt,
                user.CreatedOn,
                user.UpdatedOn
            });
        }

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="model">用户创建模型</param>
        /// <returns>创建的用户信息</returns>
        [HttpPost]
        public async Task<IActionResult> CreateUser([FromBody] CreateUserModel model)
        {
            try
            {
                var user = await _userService.CreateUserAsync(model.Username, model.Email, model.Password);
                
                // 返回用户信息，但不包含敏感数据如密码哈希
                return CreatedAtAction(nameof(GetUser), new { id = user.Id }, new
                {
                    user.Id,
                    user.Username,
                    user.Email,
                    user.Nickname,
                    user.IsEmailVerified,
                    user.IsActive,
                    user.CreatedOn
                });
            }
            catch (ArgumentException ex)
            {
                return BadRequest(new { message = ex.Message });
            }
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <param name="model">用户更新模型</param>
        /// <returns>更新后的用户信息</returns>
        [HttpPut("{id}")]
        public async Task<IActionResult> UpdateUser(Guid id, [FromBody] UpdateUserModel model)
        {
            var user = await _userService.UpdateUserProfileAsync(id, model.Nickname, model.AvatarUrl, model.Birthday, model.LifeStage);
            if (user == null)
            {
                return NotFound();
            }

            // 返回用户信息，但不包含敏感数据如密码哈希
            return Ok(new
            {
                user.Id,
                user.Username,
                user.Email,
                user.Nickname,
                user.AvatarUrl,
                user.Birthday,
                user.LifeStage,
                user.IsEmailVerified,
                user.IsActive,
                user.LastLoginAt,
                user.CreatedOn,
                user.UpdatedOn
            });
        }

        /// <summary>
        /// 更新用户密码
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <param name="model">密码更新模型</param>
        /// <returns>更新结果</returns>
        [HttpPut("{id}/password")]
        public async Task<IActionResult> UpdatePassword(Guid id, [FromBody] UpdatePasswordModel model)
        {
            var result = await _userService.UpdateUserPasswordAsync(id, model.CurrentPassword, model.NewPassword);
            if (!result)
            {
                return BadRequest(new { message = "当前密码不正确或用户不存在" });
            }

            return Ok(new { message = "密码更新成功" });
        }

        /// <summary>
        /// 验证用户邮箱
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <returns>验证结果</returns>
        [HttpPost("{id}/verify-email")]
        public async Task<IActionResult> VerifyEmail(Guid id)
        {
            var result = await _userService.VerifyUserEmailAsync(id);
            if (!result)
            {
                return BadRequest(new { message = "验证失败，用户不存在" });
            }

            return Ok(new { message = "邮箱验证成功" });
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <returns>删除结果</returns>
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteUser(Guid id)
        {
            var result = await _userService.DeleteUserAsync(id);
            if (!result)
            {
                return NotFound();
            }

            return Ok(new { message = "用户删除成功" });
        }
    }

    /// <summary>
    /// 用户创建模型
    /// </summary>
    public class CreateUserModel
    {
        /// <summary>
        /// 用户名
        /// </summary>
        public string Username { get; set; } = string.Empty;

        /// <summary>
        /// 邮箱
        /// </summary>
        public string Email { get; set; } = string.Empty;

        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; } = string.Empty;
    }

    /// <summary>
    /// 用户更新模型
    /// </summary>
    public class UpdateUserModel
    {
        /// <summary>
        /// 昵称
        /// </summary>
        public string? Nickname { get; set; }

        /// <summary>
        /// 头像URL
        /// </summary>
        public string? AvatarUrl { get; set; }

        /// <summary>
        /// 生日
        /// </summary>
        public DateTime? Birthday { get; set; }

        /// <summary>
        /// 生命周期阶段
        /// </summary>
        public string? LifeStage { get; set; }
    }

    /// <summary>
    /// 密码更新模型
    /// </summary>
    public class UpdatePasswordModel
    {
        /// <summary>
        /// 当前密码
        /// </summary>
        public string CurrentPassword { get; set; } = string.Empty;

        /// <summary>
        /// 新密码
        /// </summary>
        public string NewPassword { get; set; } = string.Empty;
    }
}